<?php
use think\Model;
use think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


    function print_pre($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
        echo "<br/>";
        exit;
    }
    function find_admin_user($id){
        $res = db('administrator')->where('id',$id)->find();
        return $res['nickname'];
    }
    function getAccessToken($code){
        // 传入$code 拿到AccessToken
        $appid  = "wx989c952dbfa8ee8c";
        $secret = "09aa68e3a0ba23400ffc2d89f84ca213";
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid."&secret=".$secret.'&code='.$code.'&grant_type=authorization_code';
        $data = file_get_contents($url); 
        $data = json_decode($data,true);
        return $data;
    }
    function list_array($arr){
        foreach ($arr as &$its) {
            $its = hot_search_select($its['bangdan_id'],$its['teacase_id'],$its['teaarea_id'],$its['id'],$its['id']);
        }
        $arr = array_filter($arr);
        return $arr;
    }
    // 将一份数组分成3份
    function list_arr($arr){
        $array = [];
        $arr1['hid'] = $arr['hid'];
        $arr1['name'] = $arr['name'];
        $arr1['id'] = 1;
        $arr1['bdname'] = list_ct_search(1);
        $array = array_merge($array,$arr1);
        $arr2 = $arr;
        $arr2['id'] = 2;
        $arr2['bdname'] = list_ct_search(2);
        $array = array_merge($array,$arr2);
        $arr3 = $arr;
        $arr3['id'] = 3;
        $arr3['bdname'] = list_ct_search(3);
        $array = array_merge($array,$arr3);
        // for ($i=1; $i < 4; $i++) { 
        //     $arr['id'] = $i;
        //     $arr['bdname'] = list_ct_search($i);
        //     $array[] = $arr;
        // }
        return $array;
    }

    // 寻找点击榜增加有效值
    function select_click($id, $bangdan_id,$teaarea_id,$teacase_id,$openid,$locations,$expiry,$score){
        // $expiry 计算周期天数
        // $score 增加分值
        $data = [
            'its_id'=>$id, 
            'bangdan_id'=>$bangdan_id,
            'teaarea_id'=>$teaarea_id,
            'teacase_id'=>$teacase_id,
            'openid'=>$openid,
            'locations'=>$locations
        ];
        $res = db('clickbang')->where($data)->find();
        $clicktime = intval($res['clicktime']) + (60*60*24*$expiry);
        if(count($res)>0){
            if(time()>$clicktime){
                db('clickbang')->where('Id',$res['Id'])->setInc('number');
                db('clickbang')->where('Id',$res['Id'])->update(['clicktime'=>time()]); 
                setjianreninc($bangdan_id,$id,$score);
            }
        }else{
            db('clickbang')->insert(['its_id'=>$id,'bangdan_id'=>$bangdan_id,'teaarea_id'=>$teaarea_id,'teacase_id'=>$teacase_id,'openid'=>$openid,'locations'=>$locations,'clicktime'=>time()]);
           setjianreninc($bangdan_id,$id,$score);
        }
    }
    // 对应的匠人新增指数
    function setjianreninc($bangdan_id,$id,$score){
        if($bangdan_id==1){
            db('jiangren')->where('Id',$id)->setInc('score',$score);
        }
    }

    // 获取热门搜索
    function get_hot_tj($teacase_id,$num="0"){
        $array = [];
        $jr = db('jiangren')->where('teacase_id',$teacase_id)->where('bangdan_id','neq',4)->limit(1)->order('click desc')->select();
        $zp = db('zhenpin')->where('teacase_id',$teacase_id)->where('bangdan_id','neq',4)->limit(1)->order('click desc')->select();
        $pp = db('pinpai')->where('teacase_id',$teacase_id)->where('bangdan_id','neq',4)->limit(1)->order('click desc')->select();
        if($jr){
            $array = array_merge($array,$jr);
        }
        if($zp){
            $array = array_merge($array,$zp);
        }
        if($pp){
            $array = array_merge($array,$pp);
        }
        $array = arr_sort($array,'click','desc');
        if($num==0){
            return $array;
        }else if($num==1){
            $array = array_pop($array);
        }else if($num==2){
            $array = array_pop($array);
            $array = array_pop($array);
        }else{
            return null;
        }
    }

    //多维数组的按照某一个键值进行排序(默认升序排列)
    function arr_sort($array,$key,$order="asc") {
        $arr_nums = $arr = array();
        foreach ($array as $k => $v) {
            $arr_nums[$k] = $v[$key]; //将排序的键值取出
        }
        if($order == "asc") {//对键值进行排序，并保留索引
            asort($arr_nums);
        } else {
            arsort($arr_nums);
        }
        foreach ($arr_nums as $k => $v) {
            $arr[] = $array[$k];//按照跑留的索引进行赋值
        }
        return $arr;
    }
    //推荐榜 
    function hot_select_tj($teacase_id){
        $list = db('hot_search')->where('list','neq',0)->where('head',$teacase_id)->order('list asc')->select();
        if(count($list)>0){
            foreach ($list as &$item) {
                $data = hot_search_area($item['bangdan_id'],$item['teacase_id'],$item['teaarea_id'],$item['Id'],$item['frequency'],$item['list'],$item['head']);
                $item = $data;
                $area = db('case_area')->alias('c_a')->where('teacase_id',$item['teacase_id'])->join('teaarea area','c_a.teaarea_id=area.id','left')->field('area aname,area.id aid')->select();
                $item['area'] = $area;
            }
        }
        return $list;
    }

    // 热门搜索的分类三类
    function hot_list_arr($arr){
        // mp($arr);
        $array = [];
        $arr1 = $arr;
        $arr1['bangdan_id'] = 1;
        $arr1['bdname'] = list_ct_search(1);
        $array[] = $arr1;
        $arr2 = $arr;
        $arr2['bangdan_id'] = 2;
        $arr2['bdname'] = list_ct_search(2);
        $array[] = $arr2;
        $arr3 = $arr;
        $arr3['bangdan_id'] = 3;
        $arr3['bdname'] = list_ct_search(3);
        $array[] = $arr3;
        return $array;
    }
    // 分类榜单
    function list_ct_search($id=1){
        if($id==1){
            $bdname = "匠人榜";
        }else if($id==2){
            $bdname = "品牌榜";
        }else if($id==3){
            $bdname = "珍品榜";
        }else{
            $bdname = "未上榜";
        }
        return $bdname;
    }
    // 热门搜索分类（具体到词条）
    function hot_search_select($bangdan_id,$teacase_id,$teaarea_id,$its_id,$id){
        $data["id"] = $id;
        if($bangdan_id==1){
            $data['bdname'] = '匠人榜';
            if($its_id!=0){
                $order = db('jiangren')->find($its_id);
                $data['ctname'] = $order['name'];
            }
        }else if($bangdan_id==2){
            $data['bdname'] = '品牌榜';
            if($its_id!=0){
            $order = db('pinpai')->find($its_id);
            $data['ctname'] = $order['name'];
            }
        }else if($bangdan_id==3){
            $data['bdname'] = '珍品榜';
            if($its_id!=0){
            $order = db('zhenpin')->find($its_id);
            $data['ctname'] = $order['name'];
            }
        }else{
            return;
        }
        $teacase = db('teacase')->find($teacase_id);
        $data['teacase'] = $teacase['teaname'];
        $teaarea = db('teaarea')->find($teaarea_id);
        $data['areas'] = $teaarea['area'];
        return $data;
    }
    function hot_search_area($bangdan_id,$teacase_id,$teaarea_id,$id,$frequency,$list,$head="0"){
        $data["Id"] = $id;
        $data['frequency'] =$frequency;
        $data['list'] = $list;
        if($bangdan_id==1){
            $data['bdname'] = '匠人榜';
        }else if($bangdan_id==2){
            $data['bdname'] = '品牌榜';
        }else if($bangdan_id==3){
            $data['bdname'] = '珍品榜';
        }else{
            return;
        }
        $data['bangdan_id'] = $bangdan_id;
        $data['teacase_id'] = $teacase_id;
        $data['teaarea_id'] = $teaarea_id;
        $teacase = db('teacase')->find($teacase_id);
        $data['teacase'] = $teacase['teaname'];
        $teaarea = db('teaarea')->find($teaarea_id);
        $data['areas'] = $teaarea['area'];
        $data['head'] = $head;
        return $data;
    }
    // curl请求
    function http_request($url,$data=array()){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
          // POST数据
          curl_setopt($ch, CURLOPT_POST, 1);
          // 把post的变量加上
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
          $output = curl_exec($ch);
          curl_close($ch);
          return $output;
      }
    //返回茶类id和茶类名称的关联数组
    function teacase_ids()
    {
        $teacase_ids = [];//茶类id和名称的关联数组
        $res = db('teacase')->select();
        foreach ($res as $key => $value) {
            $teacase_ids[$value["id"]] = $value["teaname"];
        }
        return $teacase_ids;
    }

    function mp($arr){
        echo "<pre>";
        print_r($arr);
        exit();
    }

    // 排名筛选
    function ranking($bangdan_id,$teacase_id,$teaarea_id,$id){
        $data = ['teacase_id'=>$teacase_id,'teaarea_id'=>$teaarea_id];
        if($bangdan_id==1){
            $res = db('jiangren')->where($data)->order("score desc")->select();
        }else if($bangdan_id==2){
            $res = db('pinpai')->where($data)->order("score desc")->select();
        }else{
            $res = db('zhenpin')->where($data)->order("score desc")->select();
        }
        foreach ($res as $key => &$item) {
            if($item['id']==$id){
                return $key+1;
            }    
        }
    }
    //返回产区id和产区的关联数组,如[1=>武夷]
    function teaarea_ids()
    {
        $teaarea_ids = [];
        $res = db('teaarea')->select();
        foreach ($res as $key => $value) {
            $teaarea_ids[$value["id"]] = $value["area"];
        }
        return $teaarea_ids;
    }

    //返回榜单id和名称的关联数组,如[1=>匠人]
    function bangdan_ids()
    {
        $bangdan_ids = [];//榜单id和名称的关联数组
        $res = db('bangdan')->select();
        foreach ($res as $key => $value) {
            $bangdan_ids[$value["id"]] = $value["bdname"];
        }
        return $bangdan_ids;
    }

    //返回茶类名称和id的关联数组,如[红茶=>1]
    function ids_teacase()
    {
        $teacase_ids = [];//茶类名称和id的关联数组
        $res = db('teacase')->select();
        foreach ($res as $key => $value) {
            $teacase_ids[$value["teaname"]] = $value["id"];
        }
        return $teacase_ids;
    }

    //返回茶类产区和id的关联数组,如[武夷=>1]
    function ids_teaarea()
    {
        $teaarea_ids = [];//茶类产区和id的关联数组
        $res = db('teaarea')->select();
        foreach ($res as $key => $value) {
            $teaarea_ids[$value["area"]] = $value["id"];
        }
        return $teaarea_ids;
    }

    //返回榜单名称和id的关联数组,如[匠人=>1]
    function ids_bangdan()
    {
        $bangdan_ids = [];//榜单名称和id的关联数组
        $res = db('bangdan')->select();
        foreach ($res as $key => $value) {
            $bangdan_ids[$value["bdname"]] = $value["id"];
        }
        return $bangdan_ids;
    }

    // 返回榜单
    function getteabandang(){
        $res = db('bangdan')->select();
        return $res;
    }

    //返回茶类数组
    function getTeaCase()
    {
        $res = db("teacase")->select();
        return $res;
    }

    //返回产区数组
    function getTeaArea()
    {
        $res = db("teaarea")->select();
        return $res;
    }

    //获得榜单
    function getBangDan()
    {
        $res = db("bangdan")->select();
        return $res;
    }

    //返回某种茶类名称
    function getTeaCaseName($id)
    {
        $res = db('teacase')->where('id', $id)->find();
        return $res;
    }

    //返回某茶类产区名称
    function getTeaAreaName($id)
    {
        $res = db('teaarea')->where('id', $id)->find();
        return $res;
    }

    //返回榜单信息信息,$bangdan:榜单名称，$data查询条件
    function getChoose($bangdan,$data)
    {
        $res = db($bangdan)->where($data)->order('score desc')->select();
        return $res;
    }


    function Ingenuity_year_calculation($cyear){
        // 从业年限
        $cyear_sum = 0;
        $cyear_max_sum = 10*300 + 5*100+5*150+5*200+5*250;
        if($cyear<=5){
            $cyear_sum = $cyear*100;
        }else if($cyear>5&&$cyear<=10){
            $cyear_sum = ($cyear-5)*150 + 5*100;
        }else if($cyear>10&&$cyear<=15){
            $cyear_sum = ($cyear-10)*200 + 5*100+5*150;
        }else if($cyear>15&&$cyear<=20){
            $cyear_sum = ($cyear-15)*250 + 5*100+5*150+5*200;
        }else if($cyear>20&&$cyear<=30){
            $cyear_sum = ($cyear-20)*300 + 5*100+5*150+5*200+5*250;
        }
        if($cyear>30){
            $cyear_sum =10*300 + 5*100+5*150+5*200+5*250;
        }
        if($cyear_sum>$cyear_max_sum){
            $cyear_sum = $cyear_max_sum;
        }
        return $cyear_sum;
    }

    function Ingenuity_Honor_calculation($name){
        // 荣誉
        $Honor_max = 3000;
        $Honor = 0;
        foreach ($name as $item) {
            if($item == "国家部委"){
                $Honor += 2000;
            }
            if($item =="省厅级"){
                $Honor += 1000;
            }
            if($item =="市局级"){
                $Honor += 500;
            }
            if($item =="县局级"){
                $Honor += 300;
            }
        }
        if($Honor>$Honor_max){
            $Honor = $Honor_max;
        }
        return $Honor;
    }
    function Ingenuity_Social_calculation($Title,$certificate){
        // 社会认可
        $number = 0;
        $certificate_Num = 0;
        $number_max = 0;
        if($Title=="高级职称"){
            $number = 1500;
        }else if($Title=="副高级职称"){
            $number = 1000;
        }else if($Title=="中级职称"){
            $number = 750;
        }else{
            $number = 0 ;
        }
        if($certificate=="1级"){
            $certificate_Num = 1000;
        }else if($certificate=="2级"){
            $certificate_Num = 750;
        }else if($certificate=="3级"){
            $certificate_Num = 400;
        }else if($certificate=="4级"){
            $certificate_Num = 200;
        }else if($certificate=="5级"){
            $certificate_Num = 100;
        }
        $number_max = $certificate_Num+$number;
        if($number_max>1500){
            $number_max = 1500;
        }
        return $number_max;
    }

    function Ingenuity_identity_calculation($name){
        // 社会身份
        $identity = 0;
        $identity_max = 2000;
        foreach ($name as $item) {
            if($item == "会长"){
                $identity += 2000;
            }
            if($item =="副会长"){
                $identity += 500;
            }
            if($item =="理事"){
                $identity += 200;
            }
            if($item =="会员"){
                $identity += 100;
            }
        }
        if($identity>$identity_max){
            $identity = $identity_max;
        }
        return $identity;
    }

    /***************************************************************/
    //计算工作年限
    function seniority($string)
    {
        // $pattern = '/出生于(\d{4})/';
        $pattern = '/(\d{4})/';
        preg_match_all($pattern, $string, $born);
        if(!$born[1]){
            return 0;
        }
        $now = date("Y");
        $cyear = $now - $born[1][0];
        if($cyear<0){
            $cyear = 0;
        }
        if($cyear>30){
            $cyear = 30;
        }
        $point = Ingenuity_year_calculation($cyear);
        return $point;
    }


    //计算荣誉得分
    function honorPoint($string)
    {
        //国家部委2000 省厅级1000 市局级500 县局级300 封顶3000
        $point = 0;
        $string = $string;

        $pattern = "/国家.{0,22}委/";//一个中文算三个字符，22可以匹配七个字符
        $reg = reg($pattern, $string);
        if($reg){
            $point1 = 2000;
            $point += $point1;
            // echo "国委:".$point1."<br>";
        }

        $pattern = "/省.{0,22}委/";
        $reg = reg($pattern, $string);
        if($reg){
            $point2 = 1000;
            $point += $point2;
            // echo "省委:".$point2."<br>";
        }

        $pattern = "/市.{0,22}委/";
        $reg = reg($pattern, $string);
        if($reg){
            $point3 = 500;
            $point += $point3;
            // echo "市委:".$point3."<br>";
        }

        $pattern = "/县.{0,22}委/";
        $reg = reg($pattern, $string);
        if($reg){
            $point4 = 300;
            $point += $point4;
            // echo "县委:".$point4."<br>";
        }

        if($point > 3000){
            $point = 3000;
        }
        return $point;
    }

    //社会认可度
    function recognition($string)
    {
        //高级1500 副高级1000 中级750
        $point = 0;
        $string = $string;

        $pattern = "/中级/";
        $reg = reg($pattern, $string);
        if($reg){
            $point1 = 750;
            $point += $point1;
        }

        $pattern = "/副高级/";
        $reg = reg($pattern, $string);
        if($reg){
            $point2 = 1000;
            $point += $point2;
        }else{
            $pattern = "/高级/";
            $reg = reg($pattern, $string);
            if($reg){
                $point3 = 1500;
                $point += $point3;
            }
        }

        $pattern = "/评茶/";
        $reg1 = reg($pattern, $string);
        $pattern = "/加工/";
        $reg2 = reg($pattern, $string);
        $pattern = "/制茶/";
        $reg3 = reg($pattern, $string);
        if($reg1 || $reg2 || $reg3){
            $point4 = 750;
            $point += $point4;
        }

        $pattern = "/大师/";
        $reg = reg($pattern, $string);
        if($reg){
            $point5 = 1000;
            $point += $point5;
        }

        $pattern = "/王/";
        $reg = reg($pattern, $string);
        if($reg){
            $point6 = 1000;
            $point += $point6;
        }

        if($point > 1500){
            $point = 1500;
        }
        return $point;
    }

    // 社会身份
    function socialIdentity($string)
    {
        $point = 0;
        $string = $string;

        $pattern = "/会员/";
        $reg = reg($pattern, $string);
        if($reg ){
            $point1 = 100;
            $point += $point1;
        }

        $pattern = "/理事/";
        $reg = reg($pattern, $string);
        if($reg ){
            $point2 = 200;
            $point += $point2;
        }

        $pattern = "/副会长/";
        $reg = reg($pattern, $string);
        if($reg ){
            $point3 = 500;
            $point += $point3;
        }else{
            $pattern = "/会长/";
            $reg = reg($pattern, $string);
            if($reg ){
                $point4 = 2000;
                $point += $point4;
            }
        }

        if($point > 2000){
            $point = 2000;
        }
        return $point;
    }


    //正则匹配
    function reg($pattern, $string)
    {
        // preg_match ( string $pattern , string $subject) 参数1：模式, 参数2：字符串
        if(preg_match($pattern, $string)){
            return 1;
        }else{
            return 0;
        }
    }
    /*****************************************************************/
    //获取年限分数
    function established($detailintro)
    {
        $cyear = 0;
        $pattern = '/成立于(\d{4})/';
        preg_match_all($pattern, $detailintro, $year);
         // print_pre($year);
        if($year[1]){
            $now = date("Y");
            $cyear = $now - $year[1][0];
        }else{
            $pattern = '/(\d{4})/';
            preg_match_all($pattern, $detailintro, $year);
            if($year[1]){
                $now = date("Y");
                $cyear = $now - $year[1][0];
            }
        }

        if($cyear <= 5){
            $point = $cyear * 200;
        }else if($cyear<=10){
            $point = 1000 + ($cyear-5) * 300;
        }else if($cyear<=15){
            $point = 2500 + ($cyear-10) * 500;
        }else if($cyear<=20){
            $point = 5000 + ($cyear-15) * 800;
        }else{
            $point = 9000;
        }
        return $point;
    }

    //零售额
    function sale($sale)
    {
        $pattern = '/100万/';
        if(preg_match($pattern, $sale)){
            $sale_point = 100;
            return $sale_point;
        }

        $pattern = '/100~300万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 250;
            return $sale_point;
        }

        $pattern = '/300~1000万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 500;
            return $sale_point;
        }

        $pattern = '/1000~3000万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 800;
            return $sale_point;
        }

        $pattern = '/3000~10000万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 1500;
            return $sale_point;
        }

        $pattern = '/1~3亿/';
        if(preg_match($pattern, $sale)){
            $sale_point = 2000;
            return $sale_point;
        }

        $pattern = '/3亿/';
        if(preg_match($pattern, $sale)){
            $sale_point = 3000;
            return $sale_point;
        }
        return 0;
    }

    //公众号得分
    function pnumber($pnumber)
    {
        if($pnumber){
            return 300;
        }
    }
    
    /*****************************************************************************/
    function evaluate($detailintro)
    {
        $pattern = '/不可多得/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 83;
            return $score ;
        }
        $pattern = '/难得一见/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 88;
            return $score ;
        }
        $pattern = '/百里挑一/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 93;
            return $score ;
        }
        $pattern = '/凤毛麟角/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 97;
            return $score ;
        }
        $pattern = '/稀世之宝/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 99;
            return $score ;
        }

        return 79;
    }

    